/*
 *  Copyright (c) 2010-2023
 */
package net.siufung.market.base.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.siufung.core.result.Result;
import net.siufung.core.result.ResultFail;
import net.siufung.core.result.ResultOk;
import net.siufung.market.base.app.controller.assembler.AppMenuAssembler;
import net.siufung.market.base.app.controller.model.appMenu.AppMenuSaveBatchDto;
import net.siufung.market.base.app.controller.model.appMenu.AppMenuDto;
import net.siufung.market.base.app.controller.model.appMenu.AppMenuSaveDto;
import net.siufung.market.base.app.controller.model.appMenu.AppMenuVo;
import net.siufung.market.base.app.controller.regulation.AppMenuRegulation;
import net.siufung.market.base.app.mapper.IAppMenuMapper;
import net.siufung.market.base.app.mapper.entity.AppMenu;
import net.siufung.market.base.app.service.IAppMenuService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 系统应用菜单表 前端控制层
 *
 * @author 陈建峰
 * @since 2023-07-16 17:45:10
 * @version 1.0.0
 */
@Api(tags = "系统应用菜单表")
@Slf4j
@AllArgsConstructor
@RestController
@RequestMapping("/sys/app/menu")
public class AppMenuController {

    private final IAppMenuService appMenuService;
    private final IAppMenuMapper appMenuMapper;

    /**
     * 获取 系统应用菜单表 列表
     * @param sysAppMenuDto 属性
     * @return Result<List<AppMenuVo>>
     * */
    @ApiOperation(value = "获取系统应用菜单表列表")
    @PostMapping(value = "/list")
    public Result<List<AppMenuVo>> list(
            @RequestBody AppMenuDto sysAppMenuDto) {
        List<AppMenu> sysAppMenuList = appMenuMapper.selectList(Wrappers.query(sysAppMenuDto.to()));
        return Result.ok(AppMenuAssembler.fromList(sysAppMenuList));
    }

    /**
     * 通过主键删除 应用信息 对象
     * @param appId 主键
     * @return 删除结果
     * */
    @ApiOperation(value = "删除应用信息对象")
    @PostMapping(value="/remove/{appId}")
    public Result<Void> removeByAppId(
            @PathVariable("appId") Integer appId) {
        AppMenuRegulation.Remove appRemoveRegulation = new AppMenuRegulation.Remove(appMenuMapper);
        appRemoveRegulation.isSatisfiedBy(appId);
        if(appMenuService.remove(
                new LambdaQueryWrapper<AppMenu>().eq(AppMenu::getAppId, appId))){
            return Result.ok(ResultOk.REMOVE);
        }
        return Result.fail(ResultFail.DATA_REMOVE_ERROR);
    }

    /**
     * 创建 系统应用菜单表
     * @param sysAppMenuSaveDto 对象属性
     * @return Result<Void>
     * */
    @ApiOperation(value = "创建 系统应用菜单表")
    @PostMapping(value="/save")
    public Result<Void> save(
            @RequestBody AppMenuSaveDto sysAppMenuSaveDto) {
        sysAppMenuSaveDto.validated();
        AppMenuRegulation.Save sysAppMenuSaveRegulation = new AppMenuRegulation.Save(appMenuMapper);
        sysAppMenuSaveRegulation.isSatisfiedBy(sysAppMenuSaveDto);
        if(appMenuService.save(sysAppMenuSaveDto.to())){
            return Result.ok(ResultOk.ADD);
        }
        return Result.fail(ResultFail.DATA_ADD_ERROR);
    }

    /**
     * 批量创建 系统应用菜单表
     * @param appMenuSaveBatchDto 对象属性
     * @return Result<Void>
     * */
    @ApiOperation(value = "批量创建 系统应用菜单表")
    @PostMapping(value="/saveBatch")
    public Result<Void> saveBatch(
            @RequestBody AppMenuSaveBatchDto appMenuSaveBatchDto) {
        appMenuSaveBatchDto.validated();
        AppMenuRegulation.SaveBatch sysAppMenuSaveRegulation = new AppMenuRegulation.SaveBatch(appMenuMapper);
        sysAppMenuSaveRegulation.isSatisfiedBy(appMenuSaveBatchDto);
        if(appMenuService.saveBatch(appMenuSaveBatchDto.getAppId(), appMenuSaveBatchDto.toList())){
            return Result.ok(ResultOk.ADD);
        }
        return Result.fail(ResultFail.DATA_ADD_ERROR);
    }


}